package com.ljb.greendao_demo;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.ljb.greendao.gen.UserDao;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private UserDao mUserDao;
    private ListView listview;
    private EditText et_name;
    private Button load;
    private Button updata;
    private Button delete;
    private Button add;
    private EditText et_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initListeners();
        mUserDao = MyApp.getInstance().getmDaoSession().getUserDao();
    }

    private void initListeners() {
        add.setOnClickListener(this);
        delete.setOnClickListener(this);
        updata.setOnClickListener(this);
        load.setOnClickListener(this);
    }

    private void initViews() {
        listview = (ListView) findViewById(R.id.listview);
        et_name = (EditText) findViewById(R.id.et_name);
        et_id = (EditText) findViewById(R.id.et_id);
        add = (Button) findViewById(R.id.add);
        delete = (Button) findViewById(R.id.delete);
        updata = (Button) findViewById(R.id.updata);
        load = (Button) findViewById(R.id.load);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add:
                add();
                break;
            case R.id.delete:
                delete();
                break;
            case R.id.updata:
                updata();
                break;
            case R.id.load:
                String s = et_id.getText().toString().trim();
                Long id = Long.decode(s);
                load(id);
                break;
        }
    }

    //增
    private void add() {
        String name = et_name.getText().toString().trim();
        User user = new User();
        user.setName(name);
        mUserDao.insert(user);
        load();
    }

    private void delete() {
        String s = et_id.getText().toString().trim();
        Long id = Long.decode(s);
        mUserDao.deleteByKey(id);
        load();
    }

    private void updata() {
        String name = et_name.getText().toString().trim();
        String s = et_id.getText().toString().trim();
        Long id = Long.decode(s);

        User user = new User();
        user.setName(name);
        user.setId(id);

        mUserDao.update(user);
        load();
    }

    private void load(long id) {
        et_id.setText("");
        et_name.setText("");
        User user = mUserDao.load(id);
        List<User> users = new ArrayList<>();
        users.add(user);
        listview.setAdapter(new UserAdapter(users, this));
    }

    private void load() {
        et_id.setText("");
        et_name.setText("");
        List<User> users = mUserDao.loadAll();
        listview.setAdapter(new UserAdapter(users, this));
    }

    class UserAdapter extends BaseAdapter {
        private List<User> mUsers;
        private Context mContext;

        public UserAdapter(List<User> users, Context context) {
            mUsers = users;
            mContext = context;
        }

        @Override
        public int getCount() {
            return mUsers.size();
        }

        @Override
        public Object getItem(int position) {
            return mUsers.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_listview, null);
            }

            ((TextView) convertView).setText(getContent(position));
            return convertView;
        }

        private String getContent(int position) {
            User user = (User) getItem(position);
            StringBuffer sb = new StringBuffer();
            sb.append("id:");
            sb.append(user.getId());
            sb.append("   name:");
            sb.append(user.getName());
            sb.append("\n");
            return sb.toString();
        }
    }
}
